home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / LSEARCH.C < prev    next >
Encoding:
Text File  |  1991-08-05  |  1.4 KB  |  46 lines

  1. /* lsearch on page 316 of the Turbo C Bible */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int mycompare(const void *, const void *);
  6. char *our_table[20] =
  7. {
  8.       "Microsoft C 5.1",
  9.       "Quick C 1.0",
  10.       "Turbo C 2.0",
  11.       NULL
  12. };
  13. main(int argc, char **argv)
  14. {
  15.    unsigned int i, count, oldcount;
  16.    char **p_table, **result;
  17.    if(argc < 2)
  18.    {
  19.       printf("Usage: %s <KEYWORD>\n", argv[0]);
  20.       exit(0);
  21.    }
  22.                    /* Find length of our table and print it */
  23.    printf("==== Our table contains ====\n");
  24.    for(count = 0, p_table = our_table; *p_table != NULL; p_table++, count++)
  25.                         printf("%s\n", *p_table);
  26.    oldcount = count;
  27.                /* Search for the PATH variable in the environment */
  28.    result = (char **) lsearch(&argv[1], our_table, &count, sizeof(char *),
  29.                                   mycompare);
  30.    if(count == oldcount)
  31.       printf("\nFound %s in\n%s\n", argv[1], *result);
  32.    else
  33.    {
  34.       printf("\n%s was added to table\n", argv[1]);
  35.                            /* Print table again */
  36.       printf("==== Now table contains ====\n");
  37.       for(i=0; i<count; i++)
  38.      printf("%s\n", our_table[i]);
  39.    }
  40. }
  41.                        /* ------------------------- */
  42. int mycompare(const void *arg1, const void *arg2)
  43. {
  44.                /* Compare two strings up to the length of the key */
  45.    return(strncmp(*(char**)arg1, *(char**)arg2, strlen(*(char**)arg1)));
  46. }